//
//  NSString+extension.m
//  XXProject
//
//  Created by 谢曦 on 2017/4/13.
//  Copyright © 2017年 xiexi. All rights reserved.
//

#import "NSString+extension.h"

@implementation NSString (extension)
#pragma mark - public
//2016-11-20 12:20:30
+(NSString *)xx_dateTimeFromStamp:(NSString *)timeStamp{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [NSString strWithDateFormat:dateFormatter timeStamp:timeStamp];
}

// 2016-11-20
+(NSString *)xx_dateFromTimeStamp:(NSString *)timeStamp{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    return [NSString strWithDateFormat:dateFormatter timeStamp:timeStamp];

}
//时间戳转时间 12:20:30
+(NSString *)xx_timeFromTimeStamp:(NSString *)timeStamp{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    
    return [NSString strWithDateFormat:dateFormatter timeStamp:timeStamp];

}

////时间转时间戳(date-->时间戳)
+(NSString *)xx_stampFromDate:(NSDate *)time{
    NSString *stampStr = [NSString stringWithFormat:@"%ld", (long)[time timeIntervalSince1970]];
    
    return stampStr;

}
//时间转时间戳(str-->时间戳)
+(NSString *)xx_stampFromTimeStr:(NSString *)timeStr{
    NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
    
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //str -->  date
    NSDate*inputDate = [inputFormatter dateFromString:timeStr];
    return  [NSString xx_stampFromDate:inputDate];
}

//Base64加密
+(NSString *)xx_Base64CodeWithStr:(NSString *)str{
    NSData *nsdata = [str
                      dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0];
     return base64Encoded;

}
-(NSMutableAttributedString *)xx_changeDict:(NSDictionary *)changeDict withRange:(NSRange)range
{
     [self checkRange:range];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:self];
     [attributedStr addAttributes:changeDict range:range];
    return attributedStr;
    
    
}
+(NSAttributedString *)xx_attributedStringWithString:(NSString *)str andImageName:(NSString *)imageName atIndex:(NSInteger)index{
    // 创建一个富文本
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
    // 表情图片
    attchImage.image =[UIImage imageNamed:imageName];
    // 设置图片大小
    attchImage.bounds = CGRectMake(0, 0, attchImage.image.size.width*0.95, attchImage.image.size.height*0.95);
    NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
    [attriStr insertAttributedString:stringImage atIndex:index];
    return attriStr;
}
-(NSMutableAttributedString *)xx_changeDicts:(NSArray<NSDictionary<NSString *,id> *> *)dicts andRanges:(NSArray<NSValue *> *)ranges{
    NSAssert(dicts.count==ranges.count, @"两个数组不对应");
     NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:self];
[dicts enumerateObjectsUsingBlock:^(NSDictionary<NSString *,id> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    [self checkRange:  [ranges[idx] rangeValue]];

    [attributedStr addAttributes:obj range:[ranges[idx] rangeValue]];
}];
    return attributedStr;
}

-(NSMutableAttributedString *)xx_changeWithStr:(NSString *)str withDict:(NSDictionary<NSString *,id> *)changeDict{
    NSRange range=   [self rangeOfString:str];
    NSAssert(range.length!=0, @"str不存在");
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:self];
        [attributedStr addAttributes:changeDict range:range];
    return attributedStr;

}

#pragma mark - private
+(NSString *)strWithDateFormat:(NSDateFormatter *)format timeStamp:(NSString *)timeStamp{
    NSTimeInterval time=[timeStamp doubleValue];
    
    NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    if(timeStamp.length==13){
        //13位
        detaildate= [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue] / 1000];
    }
    return  [format stringFromDate:detaildate];


}

- (BOOL)checkRange:(NSRange)range{
    NSInteger loc = range.location;
    NSInteger len = range.length;
    if (loc>=0 && len>0) {
        if (range.location + range.length <= self.length) {
            return YES;
        }
        else{
            //数组越界
            
            NSAssert(0, @"The range out-of-bounds!");
            return NO;
        }
    }
    else{
        //range格式有误
        NSAssert(0, @"The range format is wrong: NSMakeRange(a,b) (a>=0,b>0). ");
        return NO;
    }
}

@end
